import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.Scanner;

/**
 * @Author ufo
 * @Description TODO
 * @Date 2021/1/11 16:40
 * @Version 1.0
 */
public class CuratorExample {
    public static void main(String[] args) throws Exception {
        // 几句样例代码

        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
        client.start();
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择：");
        int i = Integer.parseInt(sc.nextLine());
        switch (i) {
            case 1:
                //创建一个节点，初始内容为空
                client.create().forPath("/parentnull");
                break;
            case 2:
                //创建一个节点，附带初始内容
                client.create().forPath("/parentinit", "init".getBytes());
                break;
            case 3:
                // 创建一个临时节点，初始内容为空
                client.create().withMode(CreateMode.EPHEMERAL).forPath("/parenttmpnull");
                break;
            case 4:
                // 创建一个临时节点，并自动创建父节点，这个接口非常有用，开发人员经常碰到NoNodeException.
                client.create()
                        .creatingParentsIfNeeded()
                        .withMode(CreateMode.EPHEMERAL).forPath("/parenttmp");
                break;
        }
    }
}
